home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /* function prototype */
- int DumpFile(char* filename, char* content_type);
-
- main()
- {
- char* http_accept = getenv("HTTP_ACCEPT");
-
- if (http_accept != NULL)
- {
- if (strstr(http_accept, "image/jpeg"))
- {
- /* Browser understands JPEGs so show a JPEG image */
- DumpFile("lighthou.jpg", "image/jpeg");
- exit(0); /* exit the program */
- }
- else if (strstr(http_accept, "image/gif"))
- {
- /* Browser understands GIFs so show a GIF image */
- DumpFile("lighthou.gif", "image/gif");
- exit(0); /* exit the program */
- }
- }
-
- /* If we get to this point then either the HTTP_ACCEPT variable was not set
- * or the accept type was not found in the tests above. Therefore the
- * browser cannot support GIFs or JPEGs so we show some text.
- */
-
- printf("Content-type: text/plain\n\n");
- printf("Text is the only thing I can show you.\n");
- printf("Hope you weren't expecting an image or something.\n");
- exit(0);
- }
-
-
- /* function DumpFile()
- *
- * Opens a input file specified by the variable filename and dumps the
- * contents to stdout in 8K chunks. An error is displayed if the file cannot
- * be opened.
- *
- * Returns: 0 if successful,
- * 1 if failed (cannot open file).
- */
-
- int DumpFile(char* filename, char* content_type)
- {
- FILE *fp = fopen(filename, "r");
- if (fp == NULL)
- {
- printf("Content-type: text/plain\n\n");
- /* We have a problem so call the webmaster for help */
- printf("Sorry, the file '%s' cannot be opened.\n", filename);
- printf("Please report this error to the webmaster.\n");
- return(1);
- }
- else
- {
- char buf[8096];
- int nread;
- printf("Content-type: %s\n\n", content_type);
- /* Read and output file in 8K chunks at a time which is faster
- * than doing so one byte at a time.
- */
- while ((nread = fread(buf, 1, sizeof(buf), fp)) != 0)
- {
- fwrite(buf, 1, nread, stdout); /* write buffer to stdout */
- }
- fclose(fp);
- return(0);
- }
- }
-
- /*
- * end of chk-acpt.c
- */
-